﻿using System;
using System.Web.UI.WebControls.WebParts;

namespace Softlanding.ComponentModel
{
    /// <summary>
    /// Defines the localized friendly name for a property of a Web Parts control.
    /// </summary>
    [AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = true)]
    public class SLWebDisplayNameAttribute : WebDisplayNameAttribute
    {
        private readonly string _classKey;
        private readonly string _resourceKey;

        /// <summary>
        /// Initializes a new instance of the SLWebDisplayNameAttribute class
        /// </summary>
        /// <param name="classKey">The base file name of the language resource file containing a localized description name</param>
        /// <param name="resourceKey">The name half of a name/value pair in a resource file</param>
        public SLWebDisplayNameAttribute(string classKey, string resourceKey)
        {
            _classKey = classKey;
            _resourceKey = resourceKey;
        }

        /// <summary>
        /// Gets the localized name of a property to display in a PropertyGridEditorPart control
        /// </summary>
        public override string DisplayName
        {
            get
            {
                var value = CommonHelper.GetResourceString(_classKey, _resourceKey);
                if (String.IsNullOrEmpty(value))
                {
                    value = _resourceKey;
                }
                return this.DisplayNameValue = value;
            }
        }
    }
}